﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AccountsPayable
{
    public class Employee : TaxEntity
    {
        public const float TAXRATE = .0765f;
        private string fname;
        private string lname;
        private float salary;
        private float ytdTax;
        private float ytdGross;

        public static Employee Find(DataContext ctx, String taxid)
        {
            return ctx.Employees.Single(x => x.TaxId == taxid);
        }

        public Employee(String firstname, String lastname, String taxId, float salary)
            : base (taxId)
        {
            FirstName = firstname;
            LastName = lastname;
            Salary = salary;
        }

        public void Save(DataContext ctx)
        {
            ctx.Save();
        }

        public String FirstName
        {
            get { return fname; }
            set
            {
                if (value != null && Regex.IsMatch(value, @"^[A-Za-z\-\'\s]{1,30}$"))
                {
                    fname = value;
                }
                else
                {
                    throw new ArgumentException();
                }
            }
        }
        public String LastName
        {
            get { return lname; }
            set
            {
                if (value != null && Regex.IsMatch(value, @"^[A-Za-z\-\'\s]{1,30}$"))
                {
                    lname = value;
                }
                else
                {
                    throw new ArgumentException();
                }
            }
        }
        public String FullName
        {
            get { return String.Format("{0}, {1}", LastName, FirstName); }
            set
            {
                if (value != null && Regex.IsMatch(value, @"^[A-Za-z\-\'\s\,]{1,60}$"))
                {
                    String[] parts = value.Split(',');
                    if (parts.Length != 2)
                    {
                        throw new ArgumentException();
                    }
                    LastName = parts[0].Trim();
                    FirstName = parts[1].Trim();
                }
                else
                    throw new ArgumentException();
            }
        }

        protected override string TaxIdRegex
        {
            get { return @"^\d\d\d-\d\d-\d\d\d\d$"; }
        }
    
        public float Salary
        {
            get { return salary; }
            set
            {
                if (value > 1 && value < 100000)
                    salary = value;
                else
                    throw new ArgumentException();
            }
        }

        public float YtdGross
        {
            get { return ytdGross; }
        }
        public float YtdTax
        {
            get { return ytdTax; }
        }


        public override float Pay()
        {
            return Pay(Salary);
        }

        public float Pay(float amount)
        {
            if (amount > 0 && amount < 100000)
            {
                float tax = amount * TAXRATE;
                ytdTax += tax;
                ytdGross += amount;
                return amount - tax;
            }
            else
                throw new ArgumentOutOfRangeException();
        }
    
    }
}
